/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package vista;

import conexion.Conexion;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Ceballos
 */
public class Medicos extends javax.swing.JInternalFrame {

    Conexion con, query;
    ResultSet rs;
    ResultSet rs2;
    Calendar fecha = Calendar.getInstance();
    
    Connection conexion;  //  @jve:decl-index=0:
    Statement sentencia, sentenciaxreg;  //  @jve:decl-index=0:
    ResultSet resultado, resultadoxreg;

    int xregistros;
    boolean nuevoreg;
    String elcodigo = "";
    /**
     * Creates new form NewJInternalFrame
     */
    public Medicos() {
        initComponents();
        conectar();
        
        TxtId.setEnabled(false);
        Guardar.setEnabled(false);
        BtnCancelar.setEnabled(false);
      
        CboGenero.setEnabled(false);
    }
     public void conectar() {
        try {

            Class.forName("com.mysql.jdbc.Driver").newInstance();

            conexion = DriverManager.getConnection("jdbc:mysql://localhost/endoscopia", "root", "");

            sentencia = conexion.createStatement();
            sentencia.executeQuery("SELECT * FROM medico ORDER BY ID_MEDICO");

            resultado = sentencia.getResultSet();
            if (resultado.next() == false) {
               
                xregistros = 0;
            } else {
                xregistros = 1;
                resultado.last();
                actualizar();
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "Ha ocurrido un error al intentar acceder los datos de medicos!", "Error de acceso", 2);
        }
    }
       private void guardar() {

        try {
         
            if (nuevoreg == true) { 
                sentencia = conexion.createStatement();
                sentencia.executeUpdate("INSERT INTO MEDICO(NOMBRE_MEDICO,APELLIDO_MEDICO,CEDULA_MEDICO,DIRECCION_MEDICO,CIUDAD_MEDICO,CELULAR_MEDICO,TELEFONO_MEDICO,EMAIL_MEDICO,GENERO_MEDICO,ESPECIA_MEDICO) VALUES('"
                       // + TxtId.getText() // 
                        + TxtNombre.getText()
                        + "','" + TxtApellido.getText()
                        + "','" + TxtCedula.getText()
                        + "','" + TxtDireccion.getText()
                        + "','" + TxtCiudad.getText()
                        + "','" + TxtCelular.getText()
                        + "','" + TxtTelefono.getText()
                        + "','" + TxtEmail.getText()
                        + "','" + CboGenero.getSelectedItem()
                        + "','" + TxtEspecializacion.getText()
                        + "')");
                xregistros = 1;
            } else {
                sentencia.executeUpdate("UPDATE MEDICO SET "
                        + "NOMBRE_MEDICO='" + TxtNombre.getText()
                        + "',APELLIDO_MEDICO='" + TxtApellido.getText()
                        + "',CEDULA_MEDICO='" + TxtCedula.getText()
                        + "',DIRECCION_MEDICO='" + TxtDireccion.getText()
                        + "',CIUDAD_MEDICO='" + TxtCiudad.getText()
                        + "',CELULAR_MEDICO='" + TxtCelular.getText()
                        + "',TELEFONO_MEDICO='" + TxtTelefono.getText()
                        + "',EMAIL_MEDICO='" + TxtEmail.getText()
                        + "',GENERO_MEDICO='" + CboGenero.getSelectedItem()
                        + "',ESPECIA_MEDICO='" + TxtEspecializacion.getText()
                        
                        + "'WHERE ID_MEDICO='" + TxtId.getText() + "'");

            }

            sentencia.executeQuery("SELECT * FROM MEDICO ORDER BY ID_MEDICO");
            resultado = sentencia.getResultSet();

            if (nuevoreg == true) {
                resultado.last();
                actualizar();
            } else {
                elcodigo = TxtId.getText();
                encuentra();
            }
            JOptionPane.showMessageDialog(null, "Los datos del Medico han sido guardados!", "Almacenamiento de datos", 1);
            habilitaguacan();
            TxtId.setEnabled(false);

        } catch (SQLException ex) {
            Logger.getLogger(Medicos.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "A ocurrido un problema, verifique el codigo de Medico!", "Almacenamiento de datos", 1);

        }

    }
     
        private void encuentra() {
        try {

            sentenciaxreg = conexion.createStatement();
            sentenciaxreg.executeQuery("SELECT COUNT(ID_MEDICO)AS CUANTOSMEDICO FROM MEDICO");

            resultadoxreg = sentenciaxreg.getResultSet();
            resultadoxreg.last();
            resultado.first();

            for (int x = 1; x <= resultadoxreg.getInt("CUANTOSMEDICO"); x++) {
                if (resultado.getString("id_medico").equals(elcodigo) == true) {
                    break;
                }
                resultado.next();
            }

            if (resultado.isAfterLast() == true) {
                JOptionPane.showMessageDialog(null, "El registro de medico no pudo ser localizado!", "Error de busqueda", 2);
                resultado.last();
            }
            actualizar();

        } catch (SQLException | HeadlessException m) {
            m.printStackTrace();
            JOptionPane.showMessageDialog(null, "El registro del medico no pudo ser localizado!", "Error de busqueda", 2);
        }
    }
   
    public void generacodigo() {
        try {
            if (xregistros == 0) {
                TxtId.setText("00000001");
            } else {
                resultado.last();

              //  String numerocad = (resultado.getString("id_medico").substring(2));//funcion moda virtual
                 String numerocad = (resultado.getString("id_medico").substring(6));//Funcional para Metpharma, crea numeros consecutivos
                
                String cadnuevo = String.valueOf(Integer.valueOf(numerocad) + 1);
                int x;
                String ceros = ("");
                for (x = 1; x <= 7 - cadnuevo.length(); x++) {
                    ceros = ceros + "0";
                }

                TxtId.setText("" + ceros + cadnuevo);

                if (cadnuevo.length() == 7) {
                    TxtId.setText("");
                    JOptionPane.showMessageDialog(null, "No se puede generar el codigo. Se ha superado la capacidad del sistema!", "Desbordamiento de valores", 0);
                    dispose();
                }
            }
        } catch (SQLException | NumberFormatException | HeadlessException g) {
            g.printStackTrace();
        }
    }

     
      private void actualizar() {
        try {

            TxtId.setText(resultado.getString("id_medico"));
            TxtNombre.setText(resultado.getString("nombre_medico"));
            TxtApellido.setText(resultado.getString("apellido_medico"));
            TxtEmail.setText(resultado.getString("email_medico"));
            TxtCedula.setText(resultado.getString("cedula_medico"));
            TxtDireccion.setText(resultado.getString("direccion_medico"));
            TxtCiudad.setText(resultado.getString("ciudad_medico"));
            TxtCelular.setText(resultado.getString("celular_medico"));
            TxtTelefono.setText(resultado.getString("telefono_medico"));
            TxtEspecializacion.setText(resultado.getString("especia_medico"));
            CboGenero.setSelectedItem(resultado.getString("genero_medico"));
            

            try {

              //  String lafecha = ("");
                
           
            // lafecha = resultado.getString("fecha_vence").substring(8) + "/" + resultado.getString("fecha_vence").substring(5, 7) + "/" + resultado.getString("fecha_vence").substring(0, 4);
             //   jTextFieldFechaVencimiento.setText(lafecha);
               
             
             
                 

            } catch (Exception sql) {

                sql.printStackTrace();

            }

        } catch (Exception m) {
            m.printStackTrace();
        }
    }
       private void limpiar() {

        TxtId.setText("");
        TxtNombre.setText("");
        TxtApellido.setText("");
        TxtEmail.setText("");
        TxtCedula.setText("");
        TxtDireccion.setText("");
        TxtCiudad.setText("");
        TxtCelular.setText("");
        TxtTelefono.setText("");

        CboGenero.setEnabled(false);
       
    }
       public void habilitanuemod() {
   
        Nuevo.setEnabled(false);
        BtnModificar.setEnabled(false);
        Guardar.setEnabled(true);
      
        BtnCancelar.setEnabled(true);
        BtnEliminar.setEnabled(false);
        BtnPrimerArt.setEnabled(false);
        BtnAnteriorArt.setEnabled(false);
        BtnBuscarArt.setEnabled(false);
        BtnSiguienteArt.setEnabled(false);
        BtnUltimoArt.setEnabled(false);

        CboGenero.setEnabled(true);
        
    }

    public void habilitaguacan() {

   
        Nuevo.setEnabled(true);
        BtnModificar.setEnabled(true);
       
        Guardar.setEnabled(false);

        CboGenero.setEnabled(false);
     

        BtnCancelar.setEnabled(false);
        BtnEliminar.setEnabled(true);

        BtnPrimerArt.setEnabled(true);
        BtnAnteriorArt.setEnabled(true);
        BtnBuscarArt.setEnabled(true);
        BtnSiguienteArt.setEnabled(true);
        BtnUltimoArt.setEnabled(true);

       
    }

     public void habilitanuemodActualizar() {

        Nuevo.setEnabled(false);
        BtnModificar.setEnabled(false);
        Guardar.setEnabled(true);

       
        BtnCancelar.setEnabled(true);

        BtnEliminar.setEnabled(false);
        BtnPrimerArt.setEnabled(false);
        BtnAnteriorArt.setEnabled(false);

        BtnBuscarArt.setEnabled(false);
        BtnSiguienteArt.setEnabled(false);
        BtnUltimoArt.setEnabled(false);

        CboGenero.setEnabled(true);
      

    }
        public void eliminar() {

        try {
            {
                sentencia = conexion.createStatement();
                sentencia.executeUpdate("DELETE FROM MEDICO WHERE ID_MEDICO='" + TxtId.getText() + "'");
                JOptionPane.showMessageDialog(null, "Los datos del Medico han sido eliminados!", "Eliminacion de datos", 1);
                sentencia.executeQuery("SELECT * FROM MEDICO ORDER BY ID_MEDICO");
                resultado = sentencia.getResultSet();
                if (resultado.next() == false) {
                    xregistros = 0;
                    limpiar();
                    
                } else {
                    xregistros = 1;
                    resultado.last();
                    actualizar();
                }
            }
        } catch (SQLException | HeadlessException e) {
            JOptionPane.showMessageDialog(null, "Ha ocurrido un error al intentar eliminar los datos del Medico!", "Error de eliminaciòn", 2);
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        BtnPrimerArt = new javax.swing.JButton();
        BtnAnteriorArt = new javax.swing.JButton();
        BtnSiguienteArt = new javax.swing.JButton();
        BtnBuscarArt = new javax.swing.JButton();
        BtnUltimoArt = new javax.swing.JButton();
        TxtId = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        Nuevo = new javax.swing.JButton();
        BtnModificar = new javax.swing.JButton();
        Guardar = new javax.swing.JButton();
        BtnCancelar = new javax.swing.JButton();
        BtnEliminar = new javax.swing.JButton();
        BtnSalir = new javax.swing.JButton();
        TxtNombre = new javax.swing.JTextField();
        jLabelNombre = new javax.swing.JLabel();
        jLabelNombre1 = new javax.swing.JLabel();
        TxtApellido = new javax.swing.JTextField();
        TxtCedula = new javax.swing.JTextField();
        jLabelNombre2 = new javax.swing.JLabel();
        TxtDireccion = new javax.swing.JTextField();
        jLabelNombre3 = new javax.swing.JLabel();
        TxtCiudad = new javax.swing.JTextField();
        jLabelNombre4 = new javax.swing.JLabel();
        jLabelNombre5 = new javax.swing.JLabel();
        TxtCelular = new javax.swing.JTextField();
        TxtTelefono = new javax.swing.JTextField();
        jLabelNombre6 = new javax.swing.JLabel();
        jLabelNombre7 = new javax.swing.JLabel();
        TxtEmail = new javax.swing.JTextField();
        jLabelNombre8 = new javax.swing.JLabel();
        jLabelNombre9 = new javax.swing.JLabel();
        TxtEspecializacion = new javax.swing.JTextField();
        CboGenero = new javax.swing.JComboBox();

        setMaximizable(true);
        setResizable(true);
        setMaximumSize(new java.awt.Dimension(1250, 450));
        setMinimumSize(new java.awt.Dimension(1250, 450));
        setName(""); // NOI18N
        setPreferredSize(new java.awt.Dimension(1250, 450));

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setToolTipText("");
        jPanel1.setMaximumSize(new java.awt.Dimension(1200, 500));
        jPanel1.setMinimumSize(new java.awt.Dimension(1200, 500));
        jPanel1.setPreferredSize(new java.awt.Dimension(1200, 500));

        jLabel1.setText("Formulario de edicion y consulta de medicos");

        BtnPrimerArt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGENES/primero.png"))); // NOI18N
        BtnPrimerArt.setMaximumSize(new java.awt.Dimension(33, 33));
        BtnPrimerArt.setMinimumSize(new java.awt.Dimension(33, 33));
        BtnPrimerArt.setPreferredSize(new java.awt.Dimension(33, 33));
        BtnPrimerArt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnPrimerArtActionPerformed(evt);
            }
        });

        BtnAnteriorArt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGENES/anterior.png"))); // NOI18N
        BtnAnteriorArt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnAnteriorArtActionPerformed(evt);
            }
        });

        BtnSiguienteArt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGENES/siguiente.png"))); // NOI18N
        BtnSiguienteArt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnSiguienteArtActionPerformed(evt);
            }
        });

        BtnBuscarArt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGENES/BUSCAR.png"))); // NOI18N
        BtnBuscarArt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnBuscarArtActionPerformed(evt);
            }
        });

        BtnUltimoArt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGENES/ultimo.png"))); // NOI18N
        BtnUltimoArt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnUltimoArtActionPerformed(evt);
            }
        });

        TxtId.setEnabled(false);
        TxtId.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                TxtIdFocusLost(evt);
            }
        });
        TxtId.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                TxtIdKeyTyped(evt);
            }
        });

        jPanel5.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        Nuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGENES/NUEVO.png"))); // NOI18N
        Nuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NuevoActionPerformed(evt);
            }
        });

        BtnModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGENES/MODIFICAR.png"))); // NOI18N
        BtnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnModificarActionPerformed(evt);
            }
        });

        Guardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGENES/GUARDAR.png"))); // NOI18N
        Guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GuardarActionPerformed(evt);
            }
        });

        BtnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGENES/CANCELAR.png"))); // NOI18N
        BtnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCancelarActionPerformed(evt);
            }
        });

        BtnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGENES/ANULAR.png"))); // NOI18N
        BtnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnEliminarActionPerformed(evt);
            }
        });

        BtnSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGENES/SALIR.png"))); // NOI18N
        BtnSalir.setMaximumSize(new java.awt.Dimension(55, 55));
        BtnSalir.setMinimumSize(new java.awt.Dimension(55, 55));
        BtnSalir.setPreferredSize(new java.awt.Dimension(55, 55));
        BtnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(Nuevo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BtnModificar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Guardar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BtnCancelar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BtnEliminar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(BtnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(BtnCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(BtnEliminar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(Guardar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(BtnModificar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(Nuevo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(BtnSalir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        TxtNombre.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jLabelNombre.setText("NOMBRE");

        jLabelNombre1.setText("APELLIDO");

        TxtApellido.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        TxtApellido.setMaximumSize(new java.awt.Dimension(6, 23));

        TxtCedula.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        TxtCedula.setMaximumSize(new java.awt.Dimension(6, 23));
        TxtCedula.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                TxtCedulaKeyTyped(evt);
            }
        });

        jLabelNombre2.setText("CEDULA");

        TxtDireccion.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        TxtDireccion.setMaximumSize(new java.awt.Dimension(6, 23));

        jLabelNombre3.setText("DIRECCION");

        TxtCiudad.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        TxtCiudad.setMaximumSize(new java.awt.Dimension(6, 23));

        jLabelNombre4.setText("CIUDAD");

        jLabelNombre5.setText("CELULAR");

        TxtCelular.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        TxtCelular.setMaximumSize(new java.awt.Dimension(6, 23));

        TxtTelefono.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        TxtTelefono.setMaximumSize(new java.awt.Dimension(6, 23));

        jLabelNombre6.setText("TELEFONO");

        jLabelNombre7.setText("EMAIL       ");

        TxtEmail.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        TxtEmail.setMaximumSize(new java.awt.Dimension(6, 23));

        jLabelNombre8.setText("ESPECIALIZACION");

        jLabelNombre9.setText("GENERO");

        TxtEspecializacion.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        TxtEspecializacion.setMaximumSize(new java.awt.Dimension(6, 23));

        CboGenero.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MASCULINO", "FEMENINO" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(TxtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabelNombre3)
                                .addComponent(jLabelNombre2)
                                .addComponent(jLabelNombre1)
                                .addComponent(jLabelNombre)
                                .addComponent(jLabelNombre4)
                                .addComponent(jLabelNombre5)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabelNombre7)
                                    .addComponent(jLabelNombre6)))
                            .addGap(42, 42, 42)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(TxtCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(TxtDireccion, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(TxtCedula, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(TxtNombre, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(TxtApellido, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(TxtId, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(TxtCelular, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(TxtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(83, 83, 83)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelNombre8)
                            .addComponent(jLabelNombre9))
                        .addGap(42, 42, 42)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(TxtEspecializacion, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
                            .addComponent(CboGenero, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(BtnPrimerArt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BtnAnteriorArt, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BtnBuscarArt, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BtnSiguienteArt, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BtnUltimoArt, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(317, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(BtnPrimerArt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(BtnAnteriorArt, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(BtnBuscarArt, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(BtnSiguienteArt, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(BtnUltimoArt, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(TxtId, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(TxtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabelNombre)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(19, 19, 19)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabelNombre9)
                                    .addComponent(CboGenero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(TxtEspecializacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabelNombre8))))
                        .addGap(6, 6, 6)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(TxtApellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabelNombre1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(TxtCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabelNombre2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(TxtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabelNombre3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(TxtCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabelNombre4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(TxtCelular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabelNombre5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabelNombre6)
                                    .addComponent(TxtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(TxtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabelNombre7)))
                            .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(203, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 1300, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BtnPrimerArtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnPrimerArtActionPerformed
        // TODO add your handling code here:
        try {
            resultado.first();
            actualizar();
        } catch (Exception f) {
            f.printStackTrace();
        }
    }//GEN-LAST:event_BtnPrimerArtActionPerformed

    private void BtnAnteriorArtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnAnteriorArtActionPerformed
        // TODO add your handling code here:
        try {
            if (resultado.isBeforeFirst() == false) {
                resultado.previous();
                if (resultado.isBeforeFirst() == true) {
                    resultado.first();
                }
                actualizar();
            }
        } catch (Exception p) {
            p.printStackTrace();
        }
    }//GEN-LAST:event_BtnAnteriorArtActionPerformed

    private void BtnSiguienteArtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnSiguienteArtActionPerformed
        // TODO add your handling code here:
        try {
            if (resultado.isAfterLast() == false) {
                resultado.next();
                if (resultado.isAfterLast() == true) {
                    resultado.last();
                }
                actualizar();
            }
        } catch (Exception p) {
            p.printStackTrace();
        }
    }//GEN-LAST:event_BtnSiguienteArtActionPerformed

    private void BtnBuscarArtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnBuscarArtActionPerformed
        // TODO add your handling code here:
        //  JOptionPane.showMessageDialog(this, "Busqueda Faltante");

/*        new f_listarticulosViaId().setVisible(true);

        if (f_listarticulosViaId.seleccionaart == true) {
            elcodigo = f_listarticulosViaId.codigoart;
            encuentra();
        }*/

    }//GEN-LAST:event_BtnBuscarArtActionPerformed

    private void BtnUltimoArtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnUltimoArtActionPerformed
        // TODO add your handling code here:
        try {
            resultado.last();
            actualizar();
        } catch (Exception a) {
            a.printStackTrace();
        }
    }//GEN-LAST:event_BtnUltimoArtActionPerformed

    private void TxtIdFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_TxtIdFocusLost
        // TODO add your handling code here:
        if (TxtId.getText().length() > 8) {
            JOptionPane.showMessageDialog(null, "Ingrese hasta 8 numeros para el codigo de medico", "Error de ingreso", 2);
            TxtId.setText("00000000");
        }
    }//GEN-LAST:event_TxtIdFocusLost

    private void TxtIdKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TxtIdKeyTyped
        // TODO add your handling code here:
        if (TxtId.getText().length() < 13) {
            char caracter;
            caracter = evt.getKeyChar();
            if ((caracter + "").matches("[0-9]")) {
                evt.setKeyChar(caracter);
            } else {
                evt.consume();
            }
        } else {
            evt.consume();
        }
    }//GEN-LAST:event_TxtIdKeyTyped

    private void NuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NuevoActionPerformed
        // TODO add your handling code here:

        nuevoreg = true;
        limpiar();
        habilitanuemod();

        //generacodigo();

        TxtNombre.requestFocus();
       // TxtId.setEnabled(true);


    }//GEN-LAST:event_NuevoActionPerformed

    private void BtnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnModificarActionPerformed
        // TODO add your handling code here:
        nuevoreg = false;
        habilitanuemodActualizar();
        TxtNombre.requestFocus();
       
    }//GEN-LAST:event_BtnModificarActionPerformed

    private void GuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GuardarActionPerformed

      
        if (TxtNombre.getText().trim().length() == 0) {
            JOptionPane.showMessageDialog(null, "Ingrese el Nombre del Medico!", "Error de almacenamiento", 2);
            TxtNombre.requestFocus();
        } else if (TxtApellido.getText().trim().length() == 0) {
            JOptionPane.showMessageDialog(null, "Ingrese el Apellido del Medico!", "Error de almacenamiento", 2);
            TxtApellido.requestFocus();
        } else if (TxtCedula.getText().trim().length() == 0) {
            JOptionPane.showMessageDialog(null, "Ingrese la Cedula del Medico!", "Error de almacenamiento", 2);
            TxtCedula.requestFocus();
          
        }else {
            guardar();
        }
       
    }//GEN-LAST:event_GuardarActionPerformed

    private void BtnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCancelarActionPerformed
        // TODO add your handling code here:
        try {
            int respuesta = JOptionPane.showConfirmDialog(null, "Desea descartar los datos ingresados?", "Cancelar ingresos", 0, 3);
            if (respuesta == 0) {
                sentencia.executeQuery("select * from medico order by id_medico");
                resultado = sentencia.getResultSet();

                habilitaguacan();

                if (resultado.next() == false) {
                    if (nuevoreg == false) {
                        JOptionPane.showMessageDialog(null, "Los datos de este registro acaban de ser eliminados por otro usuario del sistema!", "Vendedor inexistente", 0);
                    }
                    limpiar();
                   
                } else {
                    if (nuevoreg == true) {
                        resultado.last();
                        actualizar();
                    } else {
                        elcodigo = TxtId.getText();
                        //encuentra();
                    }
                }
            }
        } catch (HeadlessException | SQLException s) {
        }
        TxtId.setEnabled(false);
    }//GEN-LAST:event_BtnCancelarActionPerformed

    private void BtnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnEliminarActionPerformed
        // TODO add your handling code here:
        int respuesta = JOptionPane.showConfirmDialog(null, "Desea eliminar el registro del Medico " + TxtId.getText().trim() + " " + "?", "Eliminar Registro", 0, 3);
        if (respuesta == 0) {
            eliminar();
        }
    }//GEN-LAST:event_BtnEliminarActionPerformed

    private void BtnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnSalirActionPerformed
        // TODO add your handling code here:
        int resp = JOptionPane.showConfirmDialog(null, "Desea salir del formulario?", "Salir del formulario", 0);
        if (resp == 0) {
            hide();
        }
    }//GEN-LAST:event_BtnSalirActionPerformed

    private void TxtCedulaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TxtCedulaKeyTyped
        // TODO add your handling code here:
          if (!Character.isDigit(evt.getKeyChar()) && !Character.isISOControl(evt.getKeyChar())) {
            Toolkit.getDefaultToolkit().beep();
            evt.consume();
        }
    }//GEN-LAST:event_TxtCedulaKeyTyped


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnAnteriorArt;
    private javax.swing.JButton BtnBuscarArt;
    private javax.swing.JButton BtnCancelar;
    private javax.swing.JButton BtnEliminar;
    private javax.swing.JButton BtnModificar;
    private javax.swing.JButton BtnPrimerArt;
    private javax.swing.JButton BtnSalir;
    private javax.swing.JButton BtnSiguienteArt;
    private javax.swing.JButton BtnUltimoArt;
    private javax.swing.JComboBox CboGenero;
    private javax.swing.JButton Guardar;
    private javax.swing.JButton Nuevo;
    private javax.swing.JTextField TxtApellido;
    private javax.swing.JTextField TxtCedula;
    private javax.swing.JTextField TxtCelular;
    private javax.swing.JTextField TxtCiudad;
    private javax.swing.JTextField TxtDireccion;
    private javax.swing.JTextField TxtEmail;
    private javax.swing.JTextField TxtEspecializacion;
    private javax.swing.JTextField TxtId;
    private javax.swing.JTextField TxtNombre;
    private javax.swing.JTextField TxtTelefono;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelNombre;
    private javax.swing.JLabel jLabelNombre1;
    private javax.swing.JLabel jLabelNombre2;
    private javax.swing.JLabel jLabelNombre3;
    private javax.swing.JLabel jLabelNombre4;
    private javax.swing.JLabel jLabelNombre5;
    private javax.swing.JLabel jLabelNombre6;
    private javax.swing.JLabel jLabelNombre7;
    private javax.swing.JLabel jLabelNombre8;
    private javax.swing.JLabel jLabelNombre9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel5;
    // End of variables declaration//GEN-END:variables
}
